package com.illumination.monitor.common.utils;


import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
import org.springframework.aop.framework.ReflectiveMethodInvocation;

import lombok.SneakyThrows;

public class ClassUtil {
	/** 获取截点的目标方法 */
	public static @SneakyThrows Method getMethod(final JoinPoint point) {
		MethodInvocationProceedingJoinPoint methodPoint = (MethodInvocationProceedingJoinPoint) point;
		Field proxy = methodPoint.getClass().getDeclaredField("methodInvocation");
		proxy.setAccessible(true);
		ReflectiveMethodInvocation j = (ReflectiveMethodInvocation) proxy.get(methodPoint);
		Method method = j.getMethod();
		return method;
	}

	/** 获取截点的目标方法的参数->进行封装成map对象 参数名:参数值 */
	public static Map<String, Object> encapsulationMethodParameters(final JoinPoint point) {
		Signature signature = point.getSignature();
		MethodSignature methodSignature = (MethodSignature) signature;
		String[] parameterNames = methodSignature.getParameterNames();
		Object[] values = point.getArgs();
		ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<String, Object>();
		if (parameterNames.length == values.length) {
			for (int i = 0, length = values.length; i < length; i++) {
				if(values[i] == null) continue;
				map.put(parameterNames[i], values[i]);
			}
		}
		return map;
	}
}
